<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class LessonController extends HomebaseController {
    //添加新课程
	public function addlesson() {
		$Coach=M('coach');
		$coach=$Coach->order('coachid')->select();
		$this->assign('coach',$coach);
		$this->display(":addlesson");
		if($_POST['lesson']&&$_POST['lessontype']&&$_POST['stime']&&$_POST['coachid']&&$_POST['sitename']&&$_POST['uplimit']){
			$Lesson=M('lesson');
			$arr=$_POST;
			$arr['sdate']=str_replace('-','',$arr['sdate']);
			$arr['stime'].=':00';
			$msg=$Lesson->add($arr);
			if($msg){
				$this->succ('showlesson');
			}
			else{
				$this->err('showlesson');
			}
		}
		else{
			echo '请添加信息！';
		}
	}
	//课程展示（修改、删除）
	public function clear(){
		$_POST=null;
		session("lessonwhere['lid']",null);
		session("lessonwhere['sdate']",null);
		session("lessonwhere['sitename']",null);
		session("lessonwhere['lessontype']",null);
		$this->showlesson();
	}
	public function showlesson() {
		$Lesson=M('lesson');
		$Coach=M('coach');
		session(array('name'=>'session_id','expire'=>3600));
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
			session("lessonwhere['lid']",$where['lid']);
		}else if(!$_POST['lid'] && session("lessonwhere['lid']")){
			$where['lid']=session("lessonwhere['lid']");
		}
		if($_POST['sdate']){
			$where['sdate']=$_POST['sdate'];
			session("lessonwhere['sdate']",$where['sdate']);
		}else if(!$_POST['sdate'] && session("lessonwhere['sdate']")){
			$where['sdate']=session("lessonwhere['sdate']");
		}else if(!$_POST && !session('lessonwhere')){
			$where['sdate']=array('egt',date('Y-m-d',strtotime('-1 day')));
		}
		if($_POST['sitename']){
			$where['sitename']=$_POST['sitename'];
			session("lessonwhere['sitename']",$where['sitename']);
		}else if(!$_POST['sitename'] && session("lessonwhere['sitename']")){
			$where['sitename']=session("lessonwhere['sitename']");
		}
		if($_POST['lessontype']){
			$where['lessontype']=$_POST['lessontype'];
			session("lessonwhere['lessontype']",$where['lessontype']);
		}else if(!$_POST['lessontype'] && session("lessonwhere['lessontype']")){
			$where['lessontype']=session("lessonwhere['lessontype']");
		}
		$count=$Lesson->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Lesson
		->where($where)
		->order('sdate,stime')
		->limit($page->firstRow , $page->listRows)
		->select();
		//截取时间，显示为'时：分'
		for($i=0;$i<count($msg);$i++){
			$msg[$i]['stime']=substr($msg[$i]['stime'],0,5);
		}
		//教练名字获取
		for($i=0;$i<count($msg);$i++){
			$coachid=$msg[$i]['coachid'];
			$m=$Coach->where("coachid='$coachid'")->select();
			$msg[$i]['coachname']=$m[0]['coachname'];
		}
		if(!$_POST['sdate'] && !session("lessonwhere['sdate']")){
			unset($where['sdate']);
		}
		$this->assign('sitename',$sitename)
		->assign('where',$where)
		->assign('msg',$msg)
		->assign("page", $page->show('Admin'));
    	$this->display(":showlesson");
    }
	public function showlesson1() {
		$Lesson=M('lesson');
        $where['sitename']='高新';
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
		}
		if($_POST['sdate']){
			$where['sdate']=$_POST['sdate'];
		}else if(!$_POST){
			$where['sdate']=array('egt',date('Y-m-d'));
		}
		if($_POST['sitename']){
			$where['sitename']=$_POST['sitename'];
		}
		if($_POST['lessontype']){
			$where['lessontype']=$_POST['lessontype'];
		}
        $count=$Lesson->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Lesson
		->where($where)
		->order('sdate,stime')
		->limit($page->firstRow , $page->listRows)
		->select();
		//截取时间，显示为'时：分'
		for($i=0;$i<count($msg);$i++){
			$msg[$i]['stime']=substr($msg[$i]['stime'],0,5);
		}
		//教练名字获取
		$Coach=M('coach');
		for($i=0;$i<count($msg);$i++){
			$coachid=$msg[$i]['coachid'];
			$m=$Coach->where("coachid='$coachid'")->select();
			$msg[$i]['coachname']=$m[0]['coachname'];
		}
		$sitename='高新';
		$this->assign('sitename',$sitename)
		->assign("page", $page->show('Admin'))
		->assign('msg',$msg);
    	$this->display(":showlesson");
    }
	public function showlesson2() {
		$Lesson=M('lesson');
		$where['sitename']='二十四城';
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
		}
		if($_POST['sdate']){
			$where['sdate']=$_POST['sdate'];
		}else if(!$_POST){
			$where['sdate']=array('egt',date('Y-m-d'));
		}
		if($_POST['sitename']){
			$where['sitename']=$_POST['sitename'];
		}
		if($_POST['lessontype']){
			$where['lessontype']=$_POST['lessontype'];
		}
		$count=$Lesson->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Lesson
		->where($where)
		->order('sdate,stime')
		->limit($page->firstRow , $page->listRows)
		->select();
		//截取时间，显示为'时：分'
		for($i=0;$i<count($msg);$i++){
			$msg[$i]['stime']=substr($msg[$i]['stime'],0,5);
		}
		//教练名字获取
		$Coach=M('coach');
		for($i=0;$i<count($msg);$i++){
			$coachid=$msg[$i]['coachid'];
			$m=$Coach->where("coachid='$coachid'")->select();
			$msg[$i]['coachname']=$m[0]['coachname'];
		}
		$sitename='二十四城';
		$this->assign('sitename',$sitename)
		->assign("page", $page->show('Admin'))
		->assign('msg',$msg);
    	$this->display(":showlesson");
    }
	public function showlesson3() {
		$Lesson=M('lesson');
        $where['sitename']='磨子桥';
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
		}
		if($_POST['sdate']){
			$where['sdate']=$_POST['sdate'];
		}else if(!$_POST){
			$where['sdate']=array('egt',date('Y-m-d'));
		}
		if($_POST['sitename']){
			$where['sitename']=$_POST['sitename'];
		}
		if($_POST['lessontype']){
			$where['lessontype']=$_POST['lessontype'];
		}
		$count=$Lesson->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Lesson
		->where($where)
		->order('sdate,stime')
		->limit($page->firstRow , $page->listRows)
		->select();
		//截取时间，显示为'时：分'
		for($i=0;$i<count($msg);$i++){
			$msg[$i]['stime']=substr($msg[$i]['stime'],0,5);
		}
		//教练名字获取
		$Coach=M('coach');
		for($i=0;$i<count($msg);$i++){
			$coachid=$msg[$i]['coachid'];
			$m=$Coach->where("coachid='$coachid'")->select();
			$msg[$i]['coachname']=$m[0]['coachname'];
		}
		$sitename='磨子桥';
		$this->assign('sitename',$sitename)
		->assign("page", $page->show('Admin'))
		->assign('msg',$msg);
    	$this->display(":showlesson");
    }
	public function showlesson4() {
		$Lesson=M('lesson');
        $where['sitename']='凯旋广场';
		if($_POST['lid']){
			$where['lid']=$_POST['lid'];
		}
		if($_POST['sdate']){
			$where['sdate']=$_POST['sdate'];
		}else if(!$_POST){
			$where['sdate']=array('egt',date('Y-m-d'));
		}
		if($_POST['sitename']){
			$where['sitename']=$_POST['sitename'];
		}
		if($_POST['lessontype']){
			$where['lessontype']=$_POST['lessontype'];
		}
		$count=$Lesson->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Lesson
		->where($where)
		->order('sdate,stime')
		->limit($page->firstRow , $page->listRows)
		->select();
		//截取时间，显示为'时：分'
		for($i=0;$i<count($msg);$i++){
			$msg[$i]['stime']=substr($msg[$i]['stime'],0,5);
		}
		//教练名字获取
		$Coach=M('coach');
		for($i=0;$i<count($msg);$i++){
			$coachid=$msg[$i]['coachid'];
			$m=$Coach->where("coachid='$coachid'")->select();
			$msg[$i]['coachname']=$m[0]['coachname'];
		}
		$sitename='凯旋广场';
		$this->assign('sitename',$sitename)
		->assign("page", $page->show('Admin'))
		->assign('msg',$msg);
    	$this->display(":showlesson");
    }
	//删除课程
	public function dellesson() {
		$lid=$_GET['lid'];
		$Lesson=M('lesson');
		$msg=$Lesson->where("lid='$lid'")->delete();//where语句中的变量需要引号
		if($msg){
			$this->succ('showlesson');
		}
	}
	//签到
	public function sign(){
		//预约成功的订单
		$where['lid']=$where3['lid']=$_GET['lid'];
		$where['status']=array('in','1,3');
		$Reserve=M('reserve');
		$Member=M('member');
		$Lesson=M('lesson');
		$uid=$Reserve->where($where)->getField('uid',true);
		$reid=$Reserve->where($where)->getField('reid',true);
		$sign=$Reserve->where($where)->getField('sign',true);
		for($i=0;$i<count($uid);$i++){
			$where2['uid']=$uid[$i];
			$uinfo[$i]=$Member->where($where2)->find();
			$uinfo[$i]['nick']=urldecode($uinfo[$i]['nick']);
			$uinfo[$i]['reid']=$reid[$i];
			$uinfo[$i]['sign']=$sign[$i];
		}
		//排队中订单
		$where3['status']=array('in','2,4');
		$uid2=$Reserve->where($where3)->order('ordertime')->getField('uid',true);
		$reid2=$Reserve->where($where3)->order('ordertime')->getField('reid',true);
		$sign2=$Reserve->where($where3)->order('ordertime')->getField('sign',true);
		for($i=0;$i<count($uid2);$i++){
			$where4['uid']=$uid2[$i];
			$uinfo2[$i]=$Member->where($where4)->find();
			$uinfo2[$i]['nick']=urldecode($uinfo2[$i]['nick']);
			$uinfo2[$i]['reid']=$reid2[$i];
			$uinfo2[$i]['sign']=$sign2[$i];
		}
		$lid=$_GET['lid'];
		$w['lid']=$lid;
		$le=$Lesson->where($w)->find();
		$this->assign('lid',$lid)
		->assign('le',$le)
		->assign('uinfo',$uinfo)
		->assign('uinfo2',$uinfo2);
		$this->display(":sign");
	}
	//提交签到信息
	public function sign_commit(){
		$sign=$_POST['sign'];
		if(!$sign){
			$sign=array();
		}
		$reid=$_POST['reid'];
		if(!$reid){
			$reid=array();
		}
		$unsignReid=array_diff($reid,$sign);
		sort($unsignReid);
		$Reserve=M('reserve');
		$Member=M('member');
		for($i=0;$i<count($sign);$i++){
			$where1['reid']=$sign[$i];
			$data1['status']=3;
			$data1['sign']=2;
			$msg1=$Reserve->where($where1)->save($data1);
		}
		for($i=0;$i<count($unsignReid);$i++){
			//将未签到的订单标记为已完成，未签到
			$where2['reid']=$unsignReid[$i];
			$data2=array('status'=>'3');
			$msg2=$Reserve->where($where2)->setField($data2);
			//查询未签到的次数
			$unsignUid=$Reserve->where($where2)->getField('uid');
			$where3['uid']=$unsignUid;
			$where3['status']=3;
			$where3['sign']=1;
			//暂时$where3['ordertime']=array('egt',);
			$unsign=$Reserve->where($where3)->count();
			//累计三次未签到，设置两周内不能约课
			if($unsign>=3){
				$day=date('U',strtotime('+1 week'));
				$msg3=$Member->where("uid='$unsignUid'")->setField('unsignday',$day);
				$msg4=$Reserve->where($where3)->setField('status',5);
			}
		}
		$where4['lid']=$_GET['lid'];
		$where4['status']=2;
		$mmm=$Reserve->where($where4)->setField('status',4);
		echo "<script>alert('操作成功！');history.go(-2);</script>";
	}
	//编辑课程
	public function editlesson() {
			$Lesson=M('lesson');
			$lid=$_GET['lid'];
			$msg=$Lesson->where("lid='$lid'")->select();
			for($i=0;$i<count($msg);$i++){
			$msg[$i]['stime']=substr($msg[$i]['stime'],0,5);
			}
			$this->assign('msg',$msg);
			$this->display(":editlesson");
			$arr=$_POST;
			
			if($_POST){
				$msg=$Lesson->where("lid='$lid'")->save($arr);
				if($msg){
					$this->succ('showlesson');
				}
				else{
					$this->err('showlesson');
				}
			}
	}
	//导出excel
	public function out_excel(){

		$where['lid']=$lid=$_GET['lid'];
		$Reserve=M('reserve');
		$Lesson=M('lesson');
		$Member=M('member');
		$lesson=$Lesson->where($where)->getField('lesson');
		$sdate=$Lesson->where($where)->getField('sdate');
		$uid=$Reserve->where($where)->getField('uid',true);
		$reid=$Reserve->where($where)->getField('reid',true);
		for($i=0;$i<count($uid);$i++){
			$where2['uid']=$uid[$i];
			$uinfo[$i]=$Member->where($where2)->find();
			$uinfo[$i]['nick']=urldecode($uinfo[$i]['nick']);
			$uinfo[$i]['reid']=$reid[$i];
		}
//引入PHPExcel库文件（路径根据自己情况）
vendor('PHPExcel.Classes.PHPExcel');
//创建对象
		//echo "hello";
		//die();
$excel = new \PHPExcel();
//Excel表格式,这里简略写了8列
$letter = array('A','B','C','D','E','F');
//表头数组
$tableheader = array(序号,会员id,订单id,姓名,所属场馆,电话);
//定义导出的文件名
$filename = date('Ymd',strtotime($sdate)).$lesson.$lid;
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
//表格数组
/*$data = array(
array('1','小王','男'),
array('2','小李','男'),
array('3','小张','女'),
array('4','小赵','女')
);*/
for($i=0;$i<count($uinfo);$i++){
$data[$i]=array($i+1,$uinfo[$i]['uid'],$uinfo[$i]['reid'],$uinfo[$i]['name'],$uinfo[$i]['s'],$uinfo[$i]['tel']);
}
//dump($data);
//die();
//填充表格信息
for ($i = 2;$i <= count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i - 2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
//创建Excel输入对象
$write = new \PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename='$filename.xls'");
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
		
	}
	//操作成功
	public function succ($m){
		$C='Lesson';
		$this->assign('m',$m)->assign('C',$C);
		$this->display(':success');
	}
	//操作失败
	public function err($m){
		$C='Lesson';
		$this->assign('m',$m)->assign('C',$C);
		$this->display(':err');
	}
}
